package tddd24.ajgallery.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.Window;

public class Viewer {
    /**
     * The message displayed to the user when the server cannot be reached or
     * returns an error.
     */
    private static final String SERVER_ERROR = "An error occurred while "
                    + "attempting to contact the server. Please check your network "
                    + "connection and try again.";
    
    private VerticalPanel parent;
    private String URL;
    private final Hidden dir = new Hidden("direction");
    
	private FormPanel form1, form2;
	private HorizontalPanel panel, panel1, panel2;
	private Image displayedImage;
	private long version = 0;
	private Button goBack;
	
	private int newWidth, newHeight; 
	
	
	public Viewer(String url, VerticalPanel parent){
		
		this.parent = parent;
		URL = url;		
		panel = new HorizontalPanel();
		displayedImage = new Image(URL);
		displayedImage.setVisible(true);
//		int left = Window.getScrollLeft();
//		int top = Window.getScrollTop();

		//if(Window.getClientHeight() < displayedImage.getHeight())
		
		form1 = new FormPanel();
		form2 = new FormPanel();
		
		panel1 = new HorizontalPanel();
		panel2 = new HorizontalPanel();
        form1.setWidget(panel1);
        form2.setWidget(panel2);
        
		form1.setAction(url);
        form1.setMethod(FormPanel.METHOD_POST);   
		form2.setAction(url);
        form2.setMethod(FormPanel.METHOD_POST);
        
        panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);    
        
		// Add a 'view' buttons.
        panel1.add(new Button("<", new ClickHandler() {
            public void onClick(ClickEvent event) {
            	fetchPicture("previous", false);
            }
        }));
        
        panel2.add(new Button(">", new ClickHandler() {
                public void onClick(ClickEvent event) {
                	fetchPicture("next", true);
                }
        }));	
        
       
       panel.add(form1);
       panel.add(displayedImage);
       panel.add(form2);
       
       panel.setVisible(false);
       
       goBack = new Button("AJ-Gallery", new ClickHandler() {
           public void onClick(ClickEvent event) {
           		back();
           }
       });	
       goBack.setVisible(false);
       
       parent.add(goBack);
       parent.add(panel);
	}
	
	public void show(){
		int h = displayedImage.getHeight(), w = displayedImage.getWidth();
		newHeight = (int)(Window.getClientHeight() * 0.8);
		newWidth = (int)( w * newHeight / h);
		
	    panel1.setHeight(Integer.toString(newHeight));
	    panel2.setHeight(Integer.toString(newHeight));
	    
		displayedImage.setPixelSize(newWidth, newHeight);	
		
		goBack.setVisible(true);
		panel.setVisible(true);
	}
	
	public void back(){
		panel.setVisible(false);
		goBack.setVisible(false);
   		parent.getWidget(0).setVisible(true);
   		parent.getWidget(3).setVisible(true);				
	}
	
	private void fetchPicture(String direction, boolean next){
		dir.setValue(direction);
		if(next){
			panel1.add(dir);	
			form1.submit(); 
		} else {
			panel2.add(dir);	
			form2.submit(); 
		}
    	version++;
    	displayedImage.setUrl(URL + "?img" + version);
    	//displayedImage = new Image(URL + "?img" + version);
		int h = displayedImage.getHeight(), w = displayedImage.getWidth();
		newHeight = (int)(Window.getClientHeight() * 0.8);
		newWidth = (int)( w * newHeight / h);
		displayedImage.setPixelSize(newWidth, newHeight);
	}
}
